package com.go.where.manager.action.system;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.go.where.manager.action.SuperAction;
import com.go.where.manager.common.Constants;
import com.go.where.manager.exception.ManagerException;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.user.OperatorInfo;
import com.go.where.manager.pojo.user.Role;
import com.go.where.manager.service.system.OperatorService;
import com.go.where.manager.service.system.RoleService;
import com.go.where.manager.util.CommonUtil;
import com.go.where.manager.util.KeyUtil;
import com.google.gson.Gson;

public class AdminAction extends SuperAction {

	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(AdminAction.class);
	@Resource(name = "operatorService")
	private OperatorService operatorService;
	@Resource(name = "roleService")
	private RoleService roleService;
	private OperatorInfo operator;
	private List<Role> roles;
	private String userId;
	private String flag;
	private OperatorInfo operatorQuery;

	public Pagination<?> page;

	Gson gson = CommonUtil.gson();

	// 添加
	public String add() {
		roles = roleService.queryAll();
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			operatorService.save(operator);
			redirectionUrl = "admin!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("保存信息异常", e);
			addActionError("保存信息异常！");
			return this.add();
		}

	}

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String list() {
		if (operatorQuery == null) {
			operatorQuery = new OperatorInfo();
			operatorQuery.setPageSize(Constants.INIT_PAGESIZE);
			operatorQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			operatorQuery.setIsRoot("1");
			page = operatorService.queryPage(operatorQuery);
		} catch (Exception e) {
			addActionError("查询信息异常" + e.getMessage());
			log.error("查询信息异常", e);
		}

		return LIST;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		operator = operatorService.queryOperatorInfoByUserId(userId);
		roles = roleService.queryAll();
		return INPUT;
	}

	public String edit() throws Exception {
		try {
			operatorService.update(operator);
			redirectionUrl = "admin!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("修改信息异常", e);
			addActionError("修改信息异常！");
			return this.toEdit();
		}
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		try {
			int re = operatorService.delete(userId);
			redirectionUrl = "admin!list.htm";
			if (re < 0) {
				addActionError("删除信息异常！");
			}
		} catch (Exception e) {
			log.error("删除信息异常", e);
			addActionError("删除信息异常！");
			return LIST;
		}
		return SUCCESS;
	}

	/**
	 * 校验用户是否存在
	 * 
	 * @return
	 */
	public String checkUser() {
		OperatorInfo op = operatorService.queryOperatorInfoByUserId(operator
				.getUserId());
		if (op == null) {
			message = "true";
		} else {
			message = "false";
		}
		return "ajax";
	}

	// 修改密码
	public String pwd() throws Exception {
		try {
			if (operator != null) {
				String password = KeyUtil.passwordEncrypt(operator
						.getPassword());
				OperatorInfo opin = operatorService
						.queryOperatorInfoByUserId(this.getCurrentOperator()
								.getUserId());
				if (!password.equals(opin.getPassword())) {
					addActionError("旧密码输入错误");
					return "pwd";
				}
				operator.setUserId(this.getCurrentOperator().getUserId());
				password = KeyUtil.passwordEncrypt(operator.getNewPassword());
				operator.setPassword(password);
				operatorService.update(operator);
				redirectionUrl = "admin!list.htm";
				return SUCCESS;
			} else {
				return "pwd";
			}
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			return "pwd";
		} catch (Exception e) {
			addActionError("密码修改失败" + e.getMessage());
			return "pwd";
		}
	}

	/**
	 * 重置用户密码
	 * 
	 * @return
	 */
	public String reset() {
		OperatorInfo op = new OperatorInfo();
		op.setUserId(userId);
		String password = KeyUtil.passwordEncrypt("123456");
		op.setPassword(password);
		try {
			operatorService.update(op);
			redirectionUrl = "admin!list.htm";
		} catch (Exception e) {
			log.error("修改信息异常", e);
			addActionError("修改信息异常！");
		}
		return SUCCESS;
	}

	public OperatorInfo getOperator() {
		return operator;
	}

	public void setOperator(OperatorInfo operator) {
		this.operator = operator;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public OperatorInfo getOperatorQuery() {
		return operatorQuery;
	}

	public void setOperatorQuery(OperatorInfo operatorQuery) {
		this.operatorQuery = operatorQuery;
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

}
